<template>
	<view class="container">
		<view class="left">
			<view class="typeitem">全部</view>
			<view class="typeitem" :class="{acitve:item.typename==typename}" @click="changeType(item.typename)"
				v-for="item,index in leftTypeData">
				<image :src="'http://localhost:8090/foodapp'+item.icon" mode="" style="width: 40px;height: 40px;">
				</image>
				<view>{{item.typename}}</view>

			</view>
		</view>
		<view class="right">
			<!-- 菜品数据列表 -->
			<uni-list>
				
				<uni-list-item v-for="item,index in typeFoodData" :key="item.name" :title="item.name" 
				:thumb="'http://localhost:8090/foodapp'+item.icon"></uni-list-item>
			</uni-list>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				typename: '',
				leftTypeData: [{
					"id": 1,
					"typename": "鱼类",
					"icon": "/images/fish.png"
				}],
				foodData: [{
					"id": 1,
					"name": "烤鸭",
					"label": "烤鸭烤鸭烤鸭",
					"icon": "/images/1.png",
					"price": 88,
					"description": "烤鸭烤鸭烤鸭烤鸭烤鸭",
					"type": "肉类"
				}],
				typeFoodData: [{
					"id": 1,
					"name": "烤鸭",
					"label": "烤鸭烤鸭烤鸭",
					"icon": "/images/1.png",
					"price": 88,
					"description": "烤鸭烤鸭烤鸭烤鸭烤鸭",
					"type": "肉类"
				}]
			}
		},
		methods: {
			changeType(typename) {
				this.typename = typename
				uni.setStorageSync('typename', typename)
			},
			// 根据菜品类别加载数据
			loadFoodByTypeName(typename) {
				this.typeFoodData = this.foodData.filter((item) => item.type == typename)
				this.$forceUpdate()
			}

		},
		onLoad() {
			uni.request({
				url: 'http://localhost:8090/foodapp/food/list',
				method: 'POST',
				data: {},
				success: res => {
					if (res.data.success) {
						this.foodData = res.data.data
						uni.showToast({
							title: '成功'
						});
					} else {
						uni.showToast({
							title: '失败',
							icon: 'none'
						});
					}
				},
				fail: () => {
					uni.showToast({
						title: '网络错误',
						icon: 'none'
					});
				},
				complete: () => {}
			});
			// 3. 编写宫格展示的数据加载代码
			uni.request({
				url: 'http://localhost:8090/foodapp/types/list',
				method: 'POST',
				data: {},
				success: res => {
					if (res.data.success) {
						this.leftTypeData = res.data.data
					} else {
						this.leftTypeData = []
					}
				},
				fail: () => {
					uni.showToast({
						title: '网络失败',
						icon: 'none'
					});
				},
				complete: () => {}
			});
		},
		onShow() {
			// 读取本地存储的typename
			this.typename = uni.getStorageSync('typename')
		},
		watch: {
			typename: {
				handler(newTypeName) {
					// TODO 加载制定类别的菜品信息
					console.log(newTypeName);
					this.loadFoodByTypeName(newTypeName)// 更新类别菜品信息
				}
			}
		}
	}
</script>

<style>
	.container{
		display: flex;
		flex-direction: row;
		height: '100%';
	}
	.right{
		flex: 1;
		background-color: #fff;
	}
	.typeitem {
		display: flex;
		flex-direction: row;
		align-items: center;
		width: 100px;
		height: 60px;
		justify-content: space-evenly;
	}

	.typeitem.acitve {
		background-color: antiquewhite;
	}
</style>